package com.pg.agent.engine.service.service.impl;

import com.pg.agent.engine.client.model.action.Action;
import com.pg.agent.engine.client.model.sop.Memory;
import com.pg.agent.engine.service.service.AIActionService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.util.StringJoiner;

@Service
@Slf4j
public class ActionServiceImpl implements AIActionService {


    @Override
    public Memory process(Action action) {

        // 包装响应值
        String responseAll = packResponse(action);
        //将里面对话的第三人称删了
        responseAll = responseAll.replace(action.getName() + ":", "");

        // 非用户打印。
        if (!action.isUser()) {
            //System.out.println(name+"("+role+"):"+responseAll);
        }
        log.info("\n" + action.getName() + "(" + action.getRole() + "):" + responseAll);
        // 存储到硬盘
        return new Memory(0, action.getRole(), action.getName(), responseAll, responseAll);
    }

    /**
     * 包装Response;
     *
     * @return String 包装好的Response
     */
    private String packResponse(Action action) {
        StringJoiner responseAll = new StringJoiner(" ");
        if (action.getResponse() != null) {
            for (String s : action.getResponse()) {
                responseAll.add(s);
            }
        }
        return responseAll.toString();
    }
}
